<?php
namespace Home\Model;
use Think\Model;
use Think\Controller;

class ApplicationFormModel extends Model{
	
	const DELETE_STATUS = -1;
	
	protected $tableName = 'application_form';
	protected $table="application_form";
   
    //申请类型
    protected $appType = array(
     '1'=>'采购申请',
     '2'=>'报修申请',
     '3'=>'报废申请',
     '4'=>'活动申请',
     '5'=>'出差申请',
     '6'=>'备用金申请',
    );
	//采购申请类型
	protected $stationeryType = array(
     '1'=>'采购申请',
    );
	
	//报修报废申请类型
	protected $rsType = array(
     '2'=>'报修申请',
     '3'=>'报废申请',
    );
	
	//活动申请类型
	protected $activityType = array(
     '4'=>'活动申请',
    );
	
	//出差申请类型
	protected $btripType = array(
     '5'=>'出差申请',
    );
	
	//备用金申请类型
	protected $reserveMoneyType = array(
     '6'=>'备用金申请',
    );
	
	//审批状态
	protected $appStatus = array(
     '-1'=>'审批不通过',
     '1'=>'审批通过',
     '2'=>'待审批',
    );
	
	
	//核查状态
	protected $checkStatus = array(
     '-1'=>'核查不通过',
     '1'=>'核查通过',
     '2'=>'待核查',
    );
	
	
	//部门列表
	protected $department = array(
     '1'=>'人事部',
     '2'=>'销售部',
     '3'=>'客服部',
     '4'=>'风控部',
    );
	
	//月份列表
	protected $month = array(
     '1'=>'一月',
     '2'=>'二月',
     '3'=>'三月',
     '4'=>'四月',
     '5'=>'五月',
     '6'=>'六月',
     '7'=>'七月',
     '8'=>'八月',
     '9'=>'九月',
     '10'=>'十月',
     '11'=>'十一月',
     '12'=>'十二月',
    );
	
	
	

    protected $_auto = array(
        array('start', 'transfer', 1, 'callback'),
        array('end', 'transfer', 1, 'callback'),
        array('user_id', 'getUserId', 1, 'callback'),
    );
    //UTC时间转换
    public function transfer($v){
      if (empty($v)) {
        return null;
      } else {
        return UTC_to_locale_time($v);
      }
    }

    //获取user_id
    public function getUserId(){
    	if(session('uid')){
    		$user_id=session('uid');
    		return $user_id;
    	}
    }

    /**
    * 返回类型 或 所有的类型
    * 
    * @return string|array
    */
    public function getType($index){
    	if (!empty($index)) {
    		return $this->appType[$index];
    	} else {
    		return $this->appType;
    	}
    }
	
	public function getStationeryType($index){
    	if (!empty($index)) {
    		return $this->stationeryType[$index];
    	} else {
    		return $this->stationeryType;
    	}
    }
	
	public function getRsType($index){
    	if (!empty($index)) {
    		return $this->rsType[$index];
    	} else {
    		return $this->rsType;
    	}
    }
	
	public function getActivityType($index){
    	if (!empty($index)) {
    		return $this->activityType[$index];
    	} else {
    		return $this->activityType;
    	}
    }
	
	public function getBtripType($index){
    	if (!empty($index)) {
    		return $this->btripType[$index];
    	} else {
    		return $this->btripType;
    	}
    }
	
	public function getReserveMoneyType($index){
    	if (!empty($index)) {
    		return $this->reserveMoneyType[$index];
    	} else {
    		return $this->reserveMoneyType;
    	}
    }
	
	public function getStatus($index){
    	if (!empty($index)) {
    		return $this->appStatus[$index];
    	} else {
    		return $this->appStatus;
    	}
    }
	
	public function getCheckStatus($index){
    	if (!empty($index)) {
    		return $this->checkStatus[$index];
    	} else {
    		return $this->checkStatus;
    	}
    }
	
	public function getAppStatus($index){
    	if (!empty($index)) {
    		return $this->appStatus[$index];
    	} else {
    		return $this->appStatus;
    	}
    }
	
	public function getDepartmentType($index){
    	if (!empty($index)) {
    		return $this->department[$index];
    	} else {
    		return $this->department;
    	}
    }
	
	public function getMonthType($index){
    	if (!empty($index)) {
    		return $this->month[$index];
    	} else {
    		return $this->month;
    	}
    }

    public function delete($ids=array()){
        // return $this->where(array('id'=>array('in', $ids )))->save(array('status'=>-1));
        $id_arr = explode(",", $ids);
        return $this->data(array('info_status'=>self::DELETE_STATUS))->where(array('id'=> array('in', $id_arr)))->save();
    }
	/**
	 * 获取有核查权限的账户
	 */
	public function getInspector(){
		$department_id = $_SESSION['account']['userInfo']['department_id'];
		$sql = "select role_id from rbac_access where module = 'MyCheck'";
		$sql = "select user_id from rbac_role_user where role_id in ($sql)";
		$sql = "select id from rbac_user where id in ($sql) and status =1";
		$sql = "select realname,user_info.user_id from user_info where user_id in ($sql) and department_id = '$department_id'";
		
		$result = D('ApplicationForm')->query($sql);
		$res = array();
		foreach($result as $k=>$v){
			$res[$k]['id']=$result[$k]['user_id'];
			$res[$k]['realname']=$result[$k]['realname'];
		}
		return $res;
	}
     /**
	  * 获取有审批权限的账户
	  */
	
	public function getApproval(){
		$department_id = $_SESSION['account']['userInfo']['department_id'];
		$sql = "select role_id from rbac_access where module = 'MyApproval'";
		$sql = "select user_id from rbac_role_user where role_id in ($sql)";
		$sql = "select id from rbac_user where id in ($sql) and status =1";
		$sql = "select realname,user_info.user_id from user_info where user_id in ($sql) and department_id = '$department_id'";
		
		$result = D('ApplicationForm')->query($sql);
		$res = array();
		foreach($result as $k=>$v){
			$res[$k]['id']=$result[$k]['user_id'];
			$res[$k]['realname']=$result[$k]['realname'];
		}
		return $res;
	}
	
	public function getAllNames(){
		//获取当前账号所在部门
		$department_id = $_SESSION['account']['userInfo']['department_id'];
		//根据部门获取当前部门所有账号姓名
		$sql = "select realname,rbac_user.id from user_info left join rbac_user on rbac_user.id = user_info.user_id where status=1 and department_id = {$department_id}";
		$result = D('ApplicationForm')->query($sql);
		
		$res = array();
		foreach($result as $k=>$v){
			$res[$k]['id']=$result[$k]['id'];
			$res[$k]['realname']=$result[$k]['realname'];
		}
		return $res;
		
	}
	
	public function getAllCrmNames(){
		//获取当前账号所在部门
		$department_id = $_SESSION['account']['userInfo']['department_id'];
		//根据部门获取当前部门所有账号姓名
			
			$url = 'User/getCrmUser';
			$res = http_get($url);
			$json_data = json_decode($res, true);
	        $result = array('list'=>$json_data["data"]["list"]);
	        
		    $res = array();
			foreach($result['list'] as $k=>$v){
				$res[]=$result['list'][$k]['realname'];
			}
			return $res;
	}
	
	/**
	 * 获取当前账号需要处理事务的消息条数
	 */
	
	public function a(){
		$count = (int)M('application_form')->where(array('approver'=>$_SESSION['account']['userInfo']['user_id'],'app_status'=>2,'check_status'=>1,'info_status'=>1))->count();
		return $count;
	}
	public function b(){
		$count = (int)M('application_form')->where(array('inspector'=>$_SESSION['account']['userInfo']['user_id'],'check_status'=>2,'info_status'=>1))->count();
		return $count;
	}
	public function c(){
		$count = (int)M('application_form')->where(array('app_creator'=>$_SESSION['account']['userInfo']['user_id'],'app_process'=>1,'info_status'=>1))->count();
		return $count;
	}
	
	/**
	 * 获取当前账号所属部门
	 */
	
	public function getSelfDepartment(){
		$getalldepartments_url = "Department/getAllDepartments";
	    $alldepartments = json_decode(http_get($getalldepartments_url),true);
	  	if($_SESSION['account']['userInfo']['department_id']==='0'){
			$belongs_department = '总部';
		}else{
			foreach($alldepartments['list'] as $key=>$val){
				if($_SESSION['account']['userInfo']['department_id']==$val['id']){
					$belongs_department = $val['name'];
				}
			}
		}
		return $belongs_department;
	}
	
	
	/**
	 * 获取当前所属部门  二维
	 */
	 
	public function getSelfDepartments(){
		$getalldepartments_url = "Department/getAllDepartments";
	    $alldepartments = json_decode(http_get($getalldepartments_url),true);
	  	if($_SESSION['account']['userInfo']['department_id']==='0'){
			$self_department = $alldepartments["list"];
			array_unshift($self_department,array('name'=>'总部')); 
		}else{
			foreach($alldepartments['list'] as $key=>$val){
				if($_SESSION['account']['userInfo']['department_id']==$val['id']){
					$self_department[0] = $alldepartments["list"][$key];
				}
			}
		}
		return $self_department;
	}
	/*
	 * 获取真实姓名
	 * return array();
	 */
	public function getUserNames(){
        $userNames = M("UserInfo")->select();
        $names = array();
        foreach($userNames as $k=>$v){
            $names[$v['user_id']] = $v["realname"];
        }
        return $names;
    }
}